$(" #example")。DataTable()返回空数组

时间:2015-06-23 23:52:09

标签: javascript jquery datatables

经过几个小时的困惑,到处寻求帮助,我现在在这里发帖。我的问题:jQuery DataTable插件' s $(selector).DataTable()总是返回一个空数组。

以最小的形式创建它,并使用DataTables 1.10.7,应用于

zero-config example

我发现var tableRef = $("#example").DataTable()会返回[]

$("#example")

按照您的预期解决

<table id="example" class="display dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="example_info" style="width: 100%;">…</table>

$("#example tr").length   // 12 rows

这种旧式API有效:

$("#browserTable").dataTable();

[<table id=​"browserTable" cellpadding=​"0" cellspacing=​"0" margin-left=​"0" border=​"1" class=​"display dataTable" aria-describedby=​"browserTable_info" role=​"grid" style=​"width:​ 1339px;​">​…​</table>​]

但是新的没有。

我不知所措。我究竟做错了什么?有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

使用小写d来创建新的DataTable实例:

public class MainActivity extends AppCompatActivity {
private static final String TAG =  MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private RecyclerView mRecyclerView;
private ArrayList<String> shoppingListItems;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
private TextView mEmptyTextView;
private ShoppingListAdapter adapter;
private ActionButton actionButton;
private MaterialDialog addItemdialog = null;
private IabHelper mHelper;
private String SKU_REMOVE_ADDS = "remove_adds_sku";
private boolean mIsRemoveAdds = false;
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) {
            // consume the gas and update the UI
            mIsRemoveAdds = true;
            Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show();
        }
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //load ads
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
//set up billing
    String publicKey = "PUBLIC_KEY";

    mHelper = new IabHelper(this,publicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            if(!result.isSuccess()){
                //error
                Log.d(TAG,"Proglem setting up in-app Billing: " + result);
            }

            //Horay, IAB is fully set up!
            Log.d(TAG,"Horay, IAB is fully set up!");
        }
    });
    queryPurchasedItems();
}
 @Override
protected void onResume() {
    super.onResume();
    isListEmpty();
    queryPurchasedItems();
}
@Override
protected void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
}
private void queryPurchasedItems() {
    //check if user has bought "remove adds"
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (result.isFailure()) {
                // handle error here
            }
            else{
                // does the user have the premium upgrade?
                mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
                // update UI accordingly
            }
        }
    };
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(id == R.id.action_remove_adds){
        mHelper.launchPurchaseFlow(this,SKU_REMOVE_ADDS,1,mPurchasedFinishedListener,"");
    }
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        // not handled, so handle it ourselves (here's where you'd
        // perform any handling of activity results not related to in-app
        // billing...
        //setContentView(R.layout.activity_main_adds);
        super.onActivityResult(requestCode, resultCode, data);
    }
    else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
        //setContentView(R.layout.activity_main_adds);
    }
}

大数据集D用于在实例化数据表后访问API:

var tableRef = $("#example").dataTable({
    // options
});

More information

答案 1 :(得分:0)

<table align="center" width="700" cellspacing="0" border="0"  class="display" id="example">
                            <thead>


                                  <TR>
                                      <TH>column A</TH>
                                      <TH>column B</TH>
                                      <TH>column C</TH>
                                      <TH>column D</TH>
                                      <TH>column E</TH> 
                                  </TR> 

                                </thead>
                                <tfoot>
                                  <TR>
                                      <TD align="center" valign="bottom"> <B> TOTAL</B> </TD>
                                      <TD></TD>
                                      <TD></TD>
                                      <TD></TD>
                                      <TD></TD>
                                  </TR>  
                                </tfoot>  

                             </table>

HTML

 proxy = urllib2.ProxyHandler({'http': '127.0.0.1:8080'})
 opener = urllib2.build_opener(proxy)
 urllib2.install_opener(opener)
 urllib2.urlopen('http://www.google.com')

答案 2 :(得分:0)

非常感谢所有提供帮助的人。 在创建一个jsfiddle来演示问题时,我失败来重新创建它。感觉羞怯,我看起来更深一点,现在感到非常自信的问题 - 丢失(选择器)。DataTable()值 - 是在异步回调中获取该值的结果,情况(有补救措施)描述here

在我的例子中,异步代码是一个websocket处理程序:我创建DataTable以响应从我的websocket服务器接收矩阵。虽然解决方案尚不清楚,但我现在意识到我最初提出的问题未能描述实际情况,因而产生误导。

感谢所有提供帮助的人!